package com.dkd.manage.controller;

import com.dkd.common.core.domain.R;
import com.dkd.manage.pojo.entity.Sku;
import com.dkd.manage.result.PageResult;
import com.dkd.manage.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "商品管理相关接口")
@RequestMapping("/manage/sku")
public class SkuController {

    @Autowired
    private SkuService skuService;

    @ApiOperation("分页查询商品列表")
    @PreAuthorize("@ss.hasPermi('manage:sku:list')")
    @GetMapping("/list")
    public R<PageResult<Sku>> list(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                   @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam(value = "skuName", required = false) String skuName) {
        pageNum = pageNum != null ? pageNum : pageIndex;
        log.info("分页查询商品列表：{}, {}, {}", pageNum, pageSize, skuName);
        PageResult<Sku> result = skuService.list(pageNum, pageSize, skuName);
        return R.ok(result);
    }

    @ApiOperation("新增商品")
    @PreAuthorize("@ss.hasPermi('manage:sku:add')")
    @PostMapping
    public R<Integer> add(@RequestBody Sku sku) {
        log.info("新增商品：{}", sku);
        Integer rows = skuService.add(sku);
        return R.ok(rows);
    }

    @ApiOperation("根据id查询商品")
    @PreAuthorize("@ss.hasPermi('manage:sku:query')")
    @GetMapping("/{id}")
    public R<Sku> getSkuById(@PathVariable Long id) {
        log.info("根据id查询商品：{}", id);
        Sku sku = skuService.getSkuById(id);
        return R.ok(sku);
    }

    @ApiOperation("修改商品")
    @PreAuthorize("@ss.hasPermi('manage:sku:edit')")
    @PutMapping
    public R<Integer> edit(@RequestBody Sku sku) {
        log.info("修改商品：{}", sku);
        Integer rows = skuService.edit(sku);
        return R.ok(rows);
    }

    @ApiOperation("根据id删除商品")
    @PreAuthorize("@ss.hasPermi('manage:sku:remove')")
    @DeleteMapping("/{ids}")
    public R<Integer> deleteSkuById(@PathVariable Long[] ids) {
        log.info("根据id删除商品：{}", ids);
        Integer rows = skuService.deleteSkuById(ids);
        return R.ok(rows);
    }

}
